<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */

include_once(ROOT_PATH."includes/common.php");
require(ROOT_PATH."includes/Tar.php");
include_once(ROOT_PATH."func/deltree.inc.php");
include_once(ROOT_PATH."class/upload.class.php");
include_once(ROOT_PATH."class/files.class.php");
include_once(ROOT_PATH."class/history.class.php");
include_once(ROOT_PATH."class/user.class.php");
 
$CURRENT_USER = new user;
$CURRENT_USER->userid=$_SESSION['s_user'];
$loader->get($CURRENT_USER);
updateLang($CURRENT_USER->lang);
$tm->assign('lang',$LANG);
$tm->assign("CURRENT_USER",$CURRENT_USER);

if (isset($fileid)) {
	$upload = new upload;
	$upload->id=$rowid;
	$loader->get($upload);
	$upload->removeFile($fileid);
	$loader->execute($upload->updateFiles());
	
	$file = new files($upload->projectid);
	$file->id=$fileid;
	$ret = $loader->get($file);
	if ($ret) {
		sendRejectMail($CURRENT_USER,$file->userid,array($file),$upload->fname);
		$file->statusid = FILE_STATUS_LOCKED;
		$file->auto = 0;
		$file->stamp=util::hashcode($file->id);
		$loader->update($file);

		$his = new history($upload->projectid);
		$his->statusid=$file->statusid;
		$his->fileid=$file->id;
		$his->userid=$CURRENT_USER->userid;
		$his->comment=$LANG['rejected'];
		$loader->save($his);

		$filename = UPLOAD_PATH."/".$rowid;
		$tar_path = TEMP_PATH."/".$rowid;
	
		if (file_exists($tar_path)) {
			debug($tar_path. " exists. Deltree.");
			deltree($tar_path);
		} 
	
		mkdir($tar_path);
	
		$tar = new Archive_Tar($filename);
		
		if ($tar->extract($tar_path)) {
			if (file_exists($tar_path."/".$file->name)) {
				unlink($tar_path."/".$file->name);
			}
			unset($tar);
			unlink($filename);

			$dir=opendir("$tar_path");
			$filelist = array();			
			while ($item = readdir($dir)) {
				if (!is_dir($item)) {
					array_push($filelist,$tar_path."/".$item);
				}
			}

			$tar = new Archive_Tar($filename,"gz");
			$tar->createModify($filelist,"",$tar_path);
		}
		deltree($tar_path);
	}
	
	
} else {
	$upload = new upload;
	$upload->id=$rowid;
	$loader->get($upload);
	$filelist = array();
	$userid;
	foreach ($upload->filesid as $fid) {
		$file = new files($upload->projectid);
		$file->id=$fid;
		$ret = $loader->get($file);
		if ($ret) {
			$userid=$file->userid;
			$file->statusid = FILE_STATUS_LOCKED;
			$file->auto = 0;
			$file->stamp=util::hashcode($file->id);
			$loader->update($file);
	
			$his = new history($upload->projectid);
			$his->statusid=$file->statusid;
			$his->fileid=$file->id;
			$his->userid=$CURRENT_USER->userid;
			$his->comment=$LANG['rejected'];
			$loader->save($his);
			
			array_push($filelist,$file);
		}
	}
	sendRejectMail($CURRENT_USER,$userid,$filelist,$upload->fname);		
	$loader->delete($upload);
	$filename = UPLOAD_PATH."/".$rowid;
	unlink($filename);
}

?>
